home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / win / mpg2w11b / mpeg2enc / putseq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  9.7 KB  |  331 lines

  1. /* putseq.c, sequence level routines                                        */
  2.  
  3. /* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include "config.h"
  33. #include "global.h"
  34.  
  35. void putseq()
  36. {
  37.   /* this routine assumes (N % M) == 0 */
  38.   int i, j, k, f, f0, n, np, nb, sxf, syf, sxb, syb;
  39.   int ipflag;
  40.   FILE *fd;
  41.   char name[256];
  42.   unsigned char *neworg[3], *newref[3];
  43.   static char ipb[5] = {' ','I','P','B','D'};
  44.  
  45.   rc_init_seq(); /* initialize rate control */
  46.  
  47.   /* sequence header, sequence extension and sequence display extension */
  48.   putseqhdr();
  49.   if (!mpeg1)
  50.   {
  51.     putseqext();
  52.     putseqdispext();
  53.   }
  54.  
  55.   /* optionally output some text data (description, copyright or whatever) */
  56.   if (strlen(id_string) > 1)
  57.     putuserdata(id_string);
  58.  
  59.   /* loop through all frames in encoding/decoding order */
  60.   for (i=0; i<nframes; i++)
  61.   {
  62.     if (!quiet)
  63.     {
  64.       fprintf(stderr,"Encoding frame %d ",i);
  65.       fflush(stderr);
  66.     }
  67.  
  68. #ifdef _MPEG_WINDOWS_
  69.     message("Encoding frame %d out of %d    ",i+1,nframes);
  70. #endif
  71.  
  72.     /* f0: lowest frame number in current GOP
  73.      *
  74.      * first GOP contains N-(M-1) frames,
  75.      * all other GOPs contain N frames
  76.      */
  77.     f0 = N*((i+(M-1))/N) - (M-1);
  78.  
  79.     if (f0<0)
  80.       f0=0;
  81.  
  82.     if (i==0 || (i-1)%M==0)
  83.     {
  84.       /* I or P frame */
  85.       for (j=0; j<3; j++)
  86.       {
  87.         /* shuffle reference frames */
  88.         neworg[j] = oldorgframe[j];
  89.         newref[j] = oldrefframe[j];
  90.         oldorgframe[j] = neworgframe[j];
  91.         oldrefframe[j] = newrefframe[j];
  92.         neworgframe[j] = neworg[j];
  93.         newrefframe[j] = newref[j];
  94.       }
  95.  
  96.       /* f: frame number in display order */
  97.       f = (i==0) ? 0 : i+M-1;
  98.       if (f>=nframes)
  99.         f = nframes - 1;
  100.  
  101.       if (i==f0) /* first displayed frame in GOP is I */
  102.       {
  103.         /* I frame */
  104.         pict_type = I_TYPE;
  105.         forw_hor_f_code = forw_vert_f_code = 15;
  106.         back_hor_f_code = back_vert_f_code = 15;
  107.  
  108.         /* n: number of frames in current GOP
  109.          *
  110.          * first GOP contains (M-1) less (B) frames
  111.          */
  112.         n = (i==0) ? N-(M-1) : N;
  113.  
  114.         /* last GOP may contain less frames */
  115.         if (n > nframes-f0)
  116.           n = nframes-f0;
  117.  
  118.         /* number of P frames */
  119.         if (i==0)
  120.           np = (n + 2*(M-1))/M - 1; /* first GOP */
  121.         else
  122.           np = (n + (M-1))/M - 1;
  123.  
  124.         /* number of B frames */
  125.         nb = n - np - 1;
  126.  
  127.         rc_init_GOP(np,nb);
  128.  
  129.         putgophdr(f0,i==0); /* set closed_GOP in first GOP only */
  130.       }
  131.       else
  132.       {
  133.         /* P frame */
  134.         pict_type = P_TYPE;
  135.         forw_hor_f_code = motion_data[0].forw_hor_f_code;
  136.         forw_vert_f_code = motion_data[0].forw_vert_f_code;
  137.         back_hor_f_code = back_vert_f_code = 15;
  138.         sxf = motion_data[0].sxf;
  139.         syf = motion_data[0].sxf;
  140.       }
  141.     }
  142.     else
  143.     {
  144.       /* B frame */
  145.       for (j=0; j<3; j++)
  146.       {
  147.         neworg[j] = auxorgframe[j];
  148.         newref[j] = auxframe[j];
  149.       }
  150.  
  151.       /* f: frame number in display order */
  152.       f = i - 1;
  153.       pict_type = B_TYPE;
  154.       n = (i-2)%M + 1; /* first B: n=1, second B: n=2, ... */
  155.       forw_hor_f_code = motion_data[n].forw_hor_f_code;
  156.       forw_vert_f_code = motion_data[n].forw_vert_f_code;
  157.       back_hor_f_code = motion_data[n].back_hor_f_code;
  158.       back_vert_f_code = motion_data[n].back_vert_f_code;
  159.       sxf = motion_data[n].sxf;
  160.       syf = motion_data[n].syf;
  161.       sxb = motion_data[n].sxb;
  162.       syb = motion_data[n].syb;
  163.     }
  164.  
  165.     temp_ref = f - f0;
  166.     frame_pred_dct = frame_pred_dct_tab[pict_type-1];
  167.     intravlc = intravlc_tab[pict_type-1];
  168.     altscan = altscan_tab[pict_type-1];
  169.  
  170.     fprintf(statfile,"\nFrame %d (#%d in display order):\n",i,f);
  171.     fprintf(statfile," picture_type=%c\n",ipb[pict_type]);
  172.     fprintf(statfile," temporal_reference=%d\n",temp_ref);
  173.     fprintf(statfile," frame_pred_frame_dct=%d\n",frame_pred_dct);
  174.     fprintf(statfile," intra_vlc_format=%d\n",intravlc);
  175.     fprintf(statfile," alternate_scan=%d\n",altscan);
  176.  
  177.     if (pict_type!=I_TYPE)
  178.     {
  179.       fprintf(statfile," forward search window: %d...%d / %d...%d\n",
  180.         -sxf,sxf,-syf,syf);
  181.       fprintf(statfile," forward vector range: %d...%d.5 / %d...%d.5\n",
  182.         -(4<<forw_hor_f_code),(4<<forw_hor_f_code)-1,
  183.         -(4<<forw_vert_f_code),(4<<forw_vert_f_code)-1);
  184.     }
  185.  
  186.     if (pict_type==B_TYPE)
  187.     {
  188.       fprintf(statfile," backward search window: %d...%d / %d...%d\n",
  189.         -sxb,sxb,-syb,syb);
  190.       fprintf(statfile," backward vector range: %d...%d.5 / %d...%d.5\n",
  191.         -(4<<back_hor_f_code),(4<<back_hor_f_code)-1,
  192.         -(4<<back_vert_f_code),(4<<back_vert_f_code)-1);
  193.     }
  194.  
  195.     sprintf(name,tplorg,f+frame0);
  196.     readframe(name,neworg);
  197.  
  198.     if (fieldpic)
  199.     {
  200.       if (!quiet)
  201.       {
  202.         fprintf(stderr,"\nfirst field  (%s) ",topfirst ? "top" : "bot");
  203.         fflush(stderr);
  204.       }
  205.  
  206.       pict_struct = topfirst ? TOP_FIELD : BOTTOM_FIELD;
  207.  
  208.       motion_estimation(oldorgframe[0],neworgframe[0],
  209.                         oldrefframe[0],newrefframe[0],
  210.                         neworg[0],newref[0],
  211.                         sxf,syf,sxb,syb,mbinfo,0,0);
  212.  
  213.       predict(oldrefframe,newrefframe,predframe,0,mbinfo);
  214.       dct_type_estimation(predframe[0],neworg[0],mbinfo);
  215.       transform(predframe,neworg,mbinfo,blocks);
  216.  
  217.       putpict(neworg[0]);
  218.  
  219.       for (k=0; k<mb_height2*mb_width; k++)
  220.       {
  221.         if (mbinfo[k].mb_type & MB_INTRA)
  222.           for (j=0; j<block_count; j++)
  223.             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  224.                          dc_prec,intra_q,mbinfo[k].mquant);
  225.         else
  226.           for (j=0;j<block_count;j++)
  227.             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  228.                              inter_q,mbinfo[k].mquant);
  229.       }
  230.  
  231.       itransform(predframe,newref,mbinfo,blocks);
  232.       calcSNR(neworg,newref);
  233.       stats();
  234.  
  235.       if (!quiet)
  236.       {
  237.         fprintf(stderr,"second field (%s) ",topfirst ? "bot" : "top");
  238.         fflush(stderr);
  239.       }
  240.  
  241.       pict_struct = topfirst ? BOTTOM_FIELD : TOP_FIELD;
  242.  
  243.       ipflag = (pict_type==I_TYPE);
  244.       if (ipflag)
  245.       {
  246.         /* first field = I, second field = P */
  247.         pict_type = P_TYPE;
  248.         forw_hor_f_code = motion_data[0].forw_hor_f_code;
  249.         forw_vert_f_code = motion_data[0].forw_vert_f_code;
  250.         back_hor_f_code = back_vert_f_code = 15;
  251.         sxf = motion_data[0].sxf;
  252.         syf = motion_data[0].sxf;
  253.       }
  254.  
  255.       motion_estimation(oldorgframe[0],neworgframe[0],
  256.                         oldrefframe[0],newrefframe[0],
  257.                         neworg[0],newref[0],
  258.                         sxf,syf,sxb,syb,mbinfo,1,ipflag);
  259.  
  260.       predict(oldrefframe,newrefframe,predframe,1,mbinfo);
  261.       dct_type_estimation(predframe[0],neworg[0],mbinfo);
  262.       transform(predframe,neworg,mbinfo,blocks);
  263.  
  264.       putpict(neworg[0]);
  265.  
  266.       for (k=0; k<mb_height2*mb_width; k++)
  267.       {
  268.         if (mbinfo[k].mb_type & MB_INTRA)
  269.           for (j=0; j<block_count; j++)
  270.             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  271.                          dc_prec,intra_q,mbinfo[k].mquant);
  272.         else
  273.           for (j=0;j<block_count;j++)
  274.             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  275.                              inter_q,mbinfo[k].mquant);
  276.       }
  277.  
  278.       itransform(predframe,newref,mbinfo,blocks);
  279.       calcSNR(neworg,newref);
  280.       stats();
  281.     }
  282.     else
  283.     {
  284.       pict_struct = FRAME_PICTURE;
  285.  
  286.       /* do motion_estimation
  287.        *
  288.        * uses source frames (...orgframe) for full pel search
  289.        * and reconstructed frames (...refframe) for half pel search
  290.        */
  291.  
  292.       motion_estimation(oldorgframe[0],neworgframe[0],
  293.                         oldrefframe[0],newrefframe[0],
  294.                         neworg[0],newref[0],
  295.                         sxf,syf,sxb,syb,mbinfo,0,0);
  296.  
  297.       predict(oldrefframe,newrefframe,predframe,0,mbinfo);
  298.       dct_type_estimation(predframe[0],neworg[0],mbinfo);
  299.       transform(predframe,neworg,mbinfo,blocks);
  300.  
  301.       putpict(neworg[0]);
  302.  
  303.       for (k=0; k<mb_height*mb_width; k++)
  304.       {
  305.         if (mbinfo[k].mb_type & MB_INTRA)
  306.           for (j=0; j<block_count; j++)
  307.             iquant_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  308.                          dc_prec,intra_q,mbinfo[k].mquant);
  309.         else
  310.           for (j=0;j<block_count;j++)
  311.             iquant_non_intra(blocks[k*block_count+j],blocks[k*block_count+j],
  312.                              inter_q,mbinfo[k].mquant);
  313.       }
  314.  
  315.       itransform(predframe,newref,mbinfo,blocks);
  316.       calcSNR(neworg,newref);
  317.       stats();
  318.     }
  319.  
  320.     sprintf(name,tplref,f+frame0);
  321.     writeframe(name,newref);
  322.  
  323.   }
  324.  
  325. #ifdef _MPEG_WINDOWS_
  326.   message("Finished sequence of %d frames!",nframes);
  327. #endif
  328.  
  329.   putseqend();
  330. }
  331.